Practical Questions - 2025
code
class NumSwap{
public static void main(String[] aargs){
int x = 55, y = 66, t;
System.out.println("Before Swapping: x: "+x+" y: "+y);
t = x;
x = y;
y = t;
System.out.println("After Swapping: x: "+x+" y: "+y);
}
}
Output:
Before Swapping: x: 55 y: 66
After Swapping: x: 66 y: 55
This Program checks if a char is vowel or consonant. A char variable is declared and a value is assigned to it. if statement used to check if it is vowel or consonant.
Code
We can have class with name main also.
class Main {
public static void main(String[] args) {
char x = 'a';
if(x == 'a'||x == 'e'||x == 'i'||x == 'o'||x == 'u'){
System.out.println("Vowel");
}else{
System.out.println("Consonant");
}
}
}
Output:
Vowel
In this program recursive function is defined to find factorial of a number. That function is defined in the same class in which main is defined, and called from main method, So, function is made static.
Code
class Factorial {
public static int fact(int f){
if(f == 0 || f == 1)
return 1;
else
return (f * fact(f-1));
}
public static void main(String[] args) {
int x = 6;
System.out.println("Factorial of "+x+ " is: "+fact(x));
}
}
Output:
Factorial of 6 is: 720
Two integer arrays are created: arr1 and arr2. arr1 is initialized with some values, the arr1 is assigned to arr2. For loop is used to display content of arr2.
code
class CopyArray {
public static void main(String[] args) {
int[] arr1 = {1,2,33,44,12,32};
int[] arr2;
arr2 = arr1;
for(int i=0;i<arr2.length;i++)
System.out.println(i+ ": "+arr2[i]);
}
}
Output
0: 1 1: 2 2: 33 3: 44 4: 12 5: 32
In this program object of StringBuilder class is created, it is mutable, but String is immutable. String is passed to StringBuilder object, reversed method called and then toString method is called and assigned to another string reservedString.
Code
class CopyArray {
public static void main(String[] args) {
String originalString = "Lhoste";
StringBuilder stringBuilder = new StringBuilder(originalString);
String reversedString = stringBuilder.reverse().toString();
System.out.println("Original String: " + originalString);
System.out.println("Reversed String: " + reversedString);
}
}
Output
Original String: Lhoste Reversed String: etsohL
A private recursive method is written. It takes two parameter, x and y, y is power of y.
Code:
class NumPowered {
private static int powermethod(int x,int y){
if(y==1)
return x;
else{
return x * powermethod(x,y-1);
}
}
public static void main(String[] args) {
int p = powermethod(7,4);
System.out.println("Power: " + p);
}
}
Output:
Power: 2401
Two methods: recursive and non-recursive methods are written to find GCD of two given numbers in this program
Code
class CommonDivisor {
static int a,b;
private static int nonrecursiveGCD(int a,int b){
while (b != 0) {
int temp = b;
b = a % b;
a = temp;
}
return a;
}
private static int recursiveGCD(int a, int b) {
if (b == 0)
return a; // base case
return recursiveGCD(b, a % b); // recursive step
}
public static void main(String[] args) {
int p = nonrecursiveGCD(96,15);
System.out.println("Non RecursivePower: " + p);
int p1 = recursiveGCD(96,15);
System.out.println("Recursive Power: " + p1);
}
}
Output
Non RecursivePower: 3 Recursive Power: 3
Adding two integers Adding two doubles Adding three integers
In thin program FileInputStream and FileOutputStream classes are used to read and write to a text file.
Code
import java.io.*;
class WriteFile{
public static void main(String[] aa){
FileOutputStream fout;
try{
fout = new FileOutputStream("hi.txt");
String text = "Hello From World, World is deteriorating day by day, and in 2025, Artificial Intelligence is becoming powerful day by day, and already deteriorated world is hampered by AI more. Humans are become slave of AI, they are ceding to AI. ";
for(int i=0;i<text.length();i++){
fout.write(text.charAt(i));
}
fout.close();
}catch(IOException e){
System.out.println("Error ! "+e.getMessage());
}
FileInputStream fin;
try{
fin = new FileInputStream("hi.txt");
int c;
while((c = fin.read())!=-1)
System.out.print((char)c);
fin.close();
}catch(IOException e){
e.printStackTrace();
}
}
}
Output
C:\Books\JaVA_IMS\File_prog>javac WriteFile.java C:\Books\JaVA_IMS\File_prog>java WriteFile Hello From World, World is deteriorating day by day, and in 2025, Artificial Intelligence is becoming powerful day by day, and already deteriorated world is hampered by AI more. Humans are become slave of AI, they are ceding to AI. C:\Books\JaVA_IMS\File_prog>
This program uses Scanner object to read a text file. Word to be searched is passed as argumen to main method from console while executing byte code. hasNext() method of scanner object is used to read each word of the file. word to be search is assigned to str variable.
Code:
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class ReadWordsScanner {
public static void main(String[] args) {
try {
File file = new File("hi.txt"); // Replace with your file path
Scanner scanner = new Scanner(file);
String str = args[0];
int count = 0;
String word="";
while (scanner.hasNext()) { // Checks if there's another token (word)
word = scanner.next(); // Reads the next token (word)
if(word.equals(str)){
++count;
}
}
System.out.println(str+ " is found!"+ count+" times");
scanner.close();
} catch (FileNotFoundException e) {
System.err.println("File not found: " + e.getMessage());
}
}
}
Output:
C:\Books\JaVA_IMS\File_prog>javac ReadWordsScanner.java
C:\Books\JaVA_IMS\File_prog>java ReadWordsScanner world
world is found!1 times
C:\Books\JaVA_IMS\File_prog>java ReadWordsScanner day
day is found!2 times
C:\Books\JaVA_IMS\File_prog>
This program uses concept of package. Package is like a folder and is created using Package keyword, must be first statement in the program. In this program two packages: packone and packtwo are created, both have display() method. And a driver class is created in which objects from both packages are created and display method is called.
package packone;
public class MyClass {
public void display() {
System.out.println("This is packone.MyClass");
}
}
package packtwo;
public class MyClass {
public void display() {
System.out.println("This is packtwo.MyClass");
}
}
public class TestClass {
public static void main(String[] args) {
// Using fully qualified names to avoid conflict
packone.MyClass obj1 = new packone.MyClass();
packtwo.MyClass obj2 = new packtwo.MyClass();
obj1.display(); // Output: This is packone.MyClass
obj2.display(); // Output: This is packtwo.MyClass
}
}
Compile
javac packone/MyClass.java packtwo/MyClass.java TestClass.java
Run
java TestClass
Theory Questions